﻿using ManyToManyEntityFramework.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ManyToManyEntityFramework.Controllers
{
    public class TeacherController : Controller
    {
        // GET: Teacher
        public ActionResult Index()
        {
            List<TeacherIndexViewModel> model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                // x gives access to Teachers
                model = db.Teachers.Select(x => new TeacherIndexViewModel()
                {
                    TeacherId = x.TeacherId,
                    Name = x.Name,
                    // s gives access to students_courses
                    Students = db.Students_Courses.Where(s => x.Teachers_Courses.Exists(tc => tc.CourseId == s.CourseId)).Select(s => new StudentIndexViewModel() 
                    {
                        Name = s.Student.Name,
                        StudentId = s.Student.StudentId,
                        grade = s.Student.Grade
                    }).Distinct().ToList()
                }).ToList();
            }
            return View();
        }
    }
}